#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: readme.sh
# Author: GID5564
# Description: readme.md
# Created Time: 03/07/22-15:54:43
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/bash

# 避免重复导入
#[[ -n $__XLIB_IMPORTED__MARKDOWN ]] && return 0
#__XLIB_IMPORTED__MARKDOWN=1

function __markdown_init__() {

    export FILE_README="${XSHELL_PATH}/README.md"
    if [[ ! -s ${FILE_README} ]];then
        [[ ! -d ${XSHELL_PATH} ]] && mkdir ${XSHELL_PATH}
        touch ${FILE_README}
    fi
    __markdown__
    import base
    import file
    
}
function write_to_file() {
    # 无需检查文件是否存在，直接写入
    echo "$1" >> $2
}


#整行
function title_to_str(){
    #标题#
    #title_to_str <content> <level>
    local content=$1
    local level=${2:-1}
    local isn=$(IsNumber ${level})
    if [[ ${isn} -ne 1 ]];then
        level=1
    fi
    printf "%*s" "${level}" '' | tr ' ' "#"
    printf " %s" ${content}
    unset content level isn 
}

function quote_to_str(){
    #引用>
    #quote_to_str <content> <level>
    local content=$1
    local level=$2
    local temp=""
    local isn=$(IsNumber ${level})
    if [[ ${isn} -ne 0 ]]||[[ -z ${level} ]];then
        level=1
    fi

    printf "%*s" "${level}" '' | tr ' ' ">"
    printf " %s" ${content}
    unset level temp isn content
}

function picture_to_str(){
    #图片/超链接
    #picture_to_str <path> [describe]
    local image_full_path=$1
    local describe=$2
    
    if [[ -z ${describe} ]];then
        printf "!(%s)" ${image_full_path}
    else
        printf "![%s](%s)" ${describe} ${image_full_path}
    fi
    unset image_full_path describe
}

function list_to_str(){
    #有序列表
    #list_to_str <num> <describe>
    local num=$1
    local describe=${2}
    local isn=$(IsNumber ${num})
    if [[ ${isn} -ne 1 ]];then
        num=1
    fi
    printf "%s. %s" ${num} ${describe}
    unset num describe isn
}


function font_to_str(){
    #字体样式
    #font_to_str <字体> <大小> <颜色> <内容>
    local face=$1
    local size=$2
    local color=$3 
    local content=$4
    local isc=$(IsColor ${color})
    if [[ ${isc} -ne 1 ]];then
        color='#000000'
    fi
    printf "<font face='%s' size=%s color=%s>%s</font>" ${face} ${size} ${color} ${content}
    unset face size color content isc
}


##♥♥
#添加文本
function set_text(){
    #全局变量CONFIG_README
    write_to_file ${1} ${FILE_README} 
}

#
function __markdown__(){
    #成对
    ITALIC=$(printf '* ') #斜体或_
    BOLD=$(printf '** ') #粗体
    IB=$(printf '*** ') #斜粗
    DELLINE=$(printf '~~ ') #删除线
    DIVIDINGLINE=$(printf '* * *') #分隔线
    HR=$(printf '== ') #高亮
    LATEXI=$(printf '$') #行内公式
    LATEXW=$(printf '$$') #整行公式，占一行
    
    CODEI=$(printf "'") #行内
    CODEW=$(printf "''' ") #代码块
    #单个
    TABLE=$(printf '|') #表格
    DISORDERLIST=$(printf '+ ') #无序列表
    RETURN=$(printf '\r') #回车
}


#设置标题
#
function set_title(){
    local title=$1
    local level=$2
    set_text "$(title_to_str ${title} ${level})"
    unset title level
}

#设置引用
function set_quote(){
    local quote=$1
    local level=$2
    set_text "$(quote_to_str ${quote} ${level})"
    unset quote level
}


#设置超链接或图片
function set_link(){
    local url=$1
    local describe=$2
    set_text "$(picture_to_str ${url} ${describe})"
    unset url describe
}

#设置字体
function set_font(){
    local face=$1
    local size=$2
    local color=$3
    local content=$4
    
    set_text "$(font_to_str ${face} ${size} ${color} ${content})"
    unset face size color content
}

#设置有序列表
function set_order_list(){
    local num=$1
    local content=$2
    set_text "$(list_to_str ${num} ${content})"
    unset num content
}

#设置无序列表
function set_disorder_list(){
    local content=$1
    set_text "${DISORDERLIST} ${content}"
    unset content
}

#设置下一级有序列表
function set_next_order(){
    local level=$1
    local num=$2
    local content=$3
    local temp1=$(printf "%*s" "${level}" '' | tr ' ' " ")
    local temp2=$(printf "%s. %s" ${num} ${content})
    set_text "${temp1}${temp2}"
    unset level num temp1 temp2 content
}

#设置下一级无序列表
function set_next_disorder(){
    local level=$1
    local content=$2
    local temp1=$(printf "%*s" "${level}" '' | tr ' ' " ")
    local temp2="${DISORDERLIST} ${content}"
    set_text "${temp1}${temp2}"
    unset level temp1 temp2 content
}

#创建README.md文件
function WriteReadMe(){
    #标题
    set_title "Shell Functions Script"
    set_title "Gitee: https://gitee.com/gid5564/aidtool" 3
    set_text "${DIVIDINGLINE}"
    set_text "${RETURN}"
    
    
    #
    set_title "简介" 2
    set_text "这是本人备忘存储仓库，是一个基于git bash|zsh的脚本函数库，基于Shell+python3开发！如果你是一个程序员，且爱好shell的话，那么，这个库应该会对你有所裨益。
"
    set_text "${RETURN}"

    #
    set_title "使用环境" 2
    set_link "https://s1.imagehub.cc/images/2023/12/06/17879785b605a85e3a4a0264d3b7001d.png" "本机配置"
    set_text "${RETURN}"

    #
    set_title "安装" 2
    local code="${CODEI} bash -c \"\$(curl -LfsS \' https://gitee.com/gid5564/aidtool/raw/master/install.sh\')\" ${CODEI}"
    set_order_list 1 "安装AidLux;"
    set_order_list 2 "控制台执行: ${code} ;"
    set_order_list 3 "重启AidLux，然后按提示进行操作."
    set_text "${RETURN}"

    #
    set_title "目录说明" 2
    
    set_disorder_list "/xlib/ : shell函数库，包括base,core,file,string,ini,git,backup,…等开发库，可以在需要时导入，如:import base ;"
    set_disorder_list "/pylib/: python函数库，主要是一些处理图片的，期待后续添加;"
    set_disorder_list "/config/: 存放配置文件"
    set_disorder_list "/data/: 存放一些数据文件，如图片,字体等"
    set_disorder_list "/boot/: 基于xlib,pylib库开发的工具,菜单,配置等; "
    set_disorder_list "/debug/: 正在编写并测试的项目。 "

    set_text "${RETURN}"



    #
    set_title "目录结构" 2
    set_link "https://s1.imagehub.cc/images/2023/12/06/f57bbebb5f7eb42a6a10fc6bbba4d9e8.jpeg" "目录结构"
    set_text "${RETURN}"
    
    #
    set_title "友情链接" 2
    set_link "https://gitee.com/bewlief/xshell"
    set_link "https://gitee.com/mo2/linux"
    set_link "https://raw.githubusercontent.com/ohmyzsh/ohmyzsh"
    
    

}



__markdown_init__
























